package com.vk.project.untils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName : FlowUtils
 * @Description : 限流工具
 * @Author : LYQ
 * @Date: 2024-02-21 09:17
 */
@Component
public class FlowUtils {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public boolean limitOnceCheck(String key, int blockTime) {
        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(key))) {
            return false;
        } else {
            stringRedisTemplate.opsForValue()
                .set(key, "", blockTime, TimeUnit.SECONDS);
            return true;
        }
    }
}
